<template>
  <div>
    <div v-if="device === 'desktop' || device === 'ipad'" class="article-main">
      <div class="news-carousel">
        <el-carousel
          ref="carousel"
          :autoplay="false"
          arrow="never"
          indicator-position="none"
          :height="height"
          @change="carouselChange"
        >
          <template v-for="item of latestNews">
            <el-carousel-item :key="'news' + item.id">
              <div class="img">
                <img :src="item.url" :alt="item.title">
              </div>
            </el-carousel-item>
          </template>
        </el-carousel>
      </div>
      <div class="news-title">
        {{ rootNews.title }}
      </div>
      <div class="carousel-footer">
        <div class="author-info">
          <div class="author-icon">
            <img :src="rootNews.authorIcon" :alt="rootNews.author">
          </div>
          <div class="author-content">
            <div class="author-name">{{ rootNews.author }}</div>
            <div class="author-duty">{{ rootNews.duty }}</div>
          </div>
        </div>
        <div class="carousel-controls">
          <el-button icon="el-icon-arrow-left" circle @click="prev" />
          <el-button icon="el-icon-arrow-right" circle @click="next" />
        </div>
      </div>
    </div>
    <div v-if="device === 'mobile'" class="article-m-main">
      <div class="news-carousel">
        <el-carousel
          ref="carousel"
          :autoplay="false"
          arrow="never"
          indicator-position="none"
          :height="height"
          @change="carouselChange"
        >
          <template v-for="item of latestNews">
            <el-carousel-item :key="'news' + item.id">
              <div class="img">
                <img :src="item.url" :alt="item.title">
              </div>
            </el-carousel-item>
          </template>
        </el-carousel>
      </div>
      <div class="news-title">
        {{ rootNews.title }}
      </div>
      <div class="carousel-footer">
        <div class="author-info">
          <div class="author-icon">
            <img :src="rootNews.authorIcon" :alt="rootNews.author">
          </div>
          <div class="author-content">
            <div class="author-name">{{ rootNews.author }}</div>
            <div class="author-duty">{{ rootNews.duty }}</div>
          </div>
        </div>
        <div class="carousel-controls">
          <el-button icon="el-icon-arrow-left" circle @click="prev" />
          <el-button icon="el-icon-arrow-right" circle @click="next" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  components: {},
  data() {
    return {
      rootNews: {}
    }
  },
  props: {
    latestNews: {
      type: Array,
      default: () => []
    },
    height: {
      type: String,
      default: () => '200px'
    }
  },
  computed: {
    ...mapState({
      device: (state) => state.app.device
    })
  },
  mounted() {
    this.rootNews = this.latestNews[0]
  },
  methods: {
    prev() {
      this.$refs.carousel.prev()
    },
    next() {
      this.$refs.carousel.next()
    },
    carouselChange(index) {
      this.rootNews = this.latestNews[index]
    }
  }
}
</script>
<style lang="scss" scoped>
.article-main {
  .news-carousel {
    border-radius: 30px;
    overflow: hidden;
    .img {
      height: 100%;
      width: 100%;
      img{
        height: 100%;
        width: 100%;
        vertical-align: middle;
      }
    }
  }
  .news-title {
    margin-top: 41px;
    font-size: 30px;
    font-weight: 500;
    color: #222222;
    line-height: 34px;
  }
  .news-text {
    margin-top: 16px;
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    line-height: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .carousel-footer{
    margin-top: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .author-info{
      display: flex;
      align-items: center;
      .author-icon{
        width: 81px;
        height: 81px;
        border-radius: 50%;
        overflow: hidden;
        img{
          width: 100%;
          height: 100%;
          vertical-align: middle;
        }
      }
      .author-content{
        margin-left: 21px;
        .author-name{
          font-size: 28px;
          font-weight: 500;
          color: #222222;
          line-height: 28px;
        }
        .author-duty{
          margin-top: 12px;
          font-size: 20px;
          font-weight: 400;
          color: #999999;
          line-height: 20px;
        }
      }
    }
    .carousel-controls {
      text-align: right;
    }
  }
}
.article-m-main {
  .news-carousel {
    border-radius: 30px;
    overflow: hidden;
    .img {
      height: 100%;
      width: 100%;
      img{
        height: 100%;
        width: 100%;
        vertical-align: middle;
      }
    }
  }
  .news-title {
    margin-top: 10px;
    font-size: 16px;
    font-weight: 500;
    color: #222222;
    line-height: 26px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .news-text {
    margin-top: 6px;
    font-size: 12px;
    font-weight: 400;
    color: #999999;
    line-height: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .carousel-footer{
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .author-info{
      display: flex;
      align-items: center;
      .author-icon{
        width: 50px;
        height: 50px;
        border-radius: 50%;
        overflow: hidden;
        img{
          width: 100%;
          height: 100%;
          vertical-align: middle;
        }
      }
      .author-content{
        margin-left: 21px;
        .author-name{
          font-size: 16px;
          font-weight: 500;
          color: #222222;
          line-height: 20px;
        }
        .author-duty{
          margin-top: 8px;
          font-size: 14px;
          font-weight: 400;
          color: #999999;
          line-height: 14px;
        }
      }
    }
    .carousel-controls {
      text-align: right;
    }
  }
}
</style>